package cn.mygweb.common.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.thymeleaf.util.StringUtils;

import cn.mygweb.admin.modules.sys.pojo.User;
import cn.mygweb.admin.modules.sys.service.AttachmentService;
import cn.mygweb.admin.modules.sys.service.MenuService;
import cn.mygweb.admin.modules.sys.service.RoleMenuService;
import cn.mygweb.admin.modules.sys.service.RoleService;
import cn.mygweb.admin.modules.sys.service.UserRoleService;
import cn.mygweb.admin.modules.sys.service.UserService;
import cn.mygweb.admin.modules.tools.service.EmailService;
import cn.mygweb.admin.modules.web.service.ColumnsService;
import cn.mygweb.admin.modules.web.service.ContentService;
import cn.mygweb.admin.modules.web.service.MessageService;
import cn.mygweb.common.service.SysVisitService;
import cn.mygweb.common.utils.RedisUtils;

/** 
* <p>Title: BaseController.java</p>
* <p>Description: 基础控制类</p>  
* @author gxb 
* @date 2019年6月26日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
public class BaseController {
	
	@Autowired
	protected Sid sid;
	
	@Autowired
	protected RedisUtils redisUtils;
	
	//文件上传地址
	@Value("${file.upload.path}")
	protected String uploadFilePath;

	@Autowired
	protected ColumnsService columnsService;
	@Autowired
	protected ContentService contentService;
	@Autowired
	protected MessageService messageService;
	@Autowired
	protected SysVisitService sysVisitService;
	@Autowired
	protected AttachmentService attachmentService;
	@Autowired
	protected MenuService menuService;
	@Autowired
	protected RoleService roleService; 
	@Autowired
	protected RoleMenuService roleMenuService;
	@Autowired
	protected UserService userService;
	@Autowired
	protected UserRoleService userRoleService;
	@Autowired
	protected EmailService emailService;
	
	
	/**
	 * <p>Title: getCurrentUserId</p>
	 * <p>Description: 获取当前登录用户的id</p>
	 * @return
	 */
	public String getCurrentUserId() {
		Subject s = SecurityUtils.getSubject();
		User user = (User)s.getPrincipal();
		return user.getId();
	}
	
	/**
	 * <p>Title: redisSet</p>
	 * <p>Description: 添加redis缓存</p>
	 * @param key
	 * @param value
	 */
	protected void redisSet(String key, String value) {
		redisUtils.set(key, value);
	}
	
	/**
	 * <p>Title: redisGet</p>
	 * <p>Description: 获取key对应的值</p>
	 * @param key
	 * @return
	 */
	protected String redisGet(String key) {
		return redisUtils.get(key);
	}
	
	/**
	 * <p>Title: delRedisCache</p>
	 * <p>Description: 删除redis中的所有缓存信息</p>
	 */
	protected void delAllRedisCache() {
		redisUtils.delAll();
	}
	
	/**
	 * <p>Title: delRedisCacheByKey</p>
	 * <p>Description: 删除指定键名称的缓存信息</p>
	 * @param key
	 */
	protected void delRedisCacheByKey(String key) {
		redisUtils.del(key);
	}
	
	/**
	 * <p>Title: delRedisCacheByKeys</p>
	 * <p>Description: 删除多个缓存信息</p>
	 * @param keys
	 */
	protected void delRedisCacheByKeys(String... keys) {
		for (String key : keys) {
			redisUtils.del(key);
		}
	}
	
	/**
	 * <p>Title: isNullOrBlank</p>
	 * <p>Description: 判断指定键值名称的缓存是否存在</p>
	 * @param obj
	 * @return	true:不存在，为空; false:存在，不为空
	 */
	protected boolean isNullOrBlank(String key) {
		return null == redisGet(key) || StringUtils.isEmptyOrWhitespace(redisGet(key));
	}
	
	/**
	 * <p>Title: isEmptyList</p>
	 * <p>Description: 判断集合是否为空</p>
	 * @param list
	 * @return
	 */
	protected <T> boolean isEmptyList(List<T> list) {
		return null == list || list.isEmpty();
	}
	
	/**
	 * <p>Title: isEmptyString</p>
	 * <p>Description: 判断字符串是否为null或为空白字符串</p>
	 * @param str
	 * @return
	 */
	protected boolean isEmptyString(String str) {
		return null == str || StringUtils.isEmptyOrWhitespace(str.trim());
	}
	
}
